<?php

/*
  
  Project: CI Forum
  Type: PHP and database
  Desc: Contains all functions related to categories.
  Author: Yash Bharadwaj
 
 

*/

class Thread_model extends Model {

    function Thread_model() {
    // Call the Model constructor
        parent::Model();
        $this->load->helper('date');
        $this->load->library('table');

    }

    
    function create_thread() {

       $forum_title=$this->uri->segment(3);
        if($this->input->post('enable_notification'))
            $notification = 1;
        else
            $notification = 0;

        if($this->input->post('make_pin'))
            $pin = 1;
        else
            $pin = 0;

        if($this->input->post('make_lock'))
            $lock = 1;
        else
            $lock = 0;

        if($this->input->post('make_annoucement'))
            $annoucement = 1;
        else
            $annoucement = 0;

        if($this->input->post('make_hidden'))
            $hidden = 1;
        else
            $hidden = 0;

        // $f_id=$this->input->post('forum_id');

        $insert = array(
            'thread_id' => '',
            'thread_title' => $this->input->post('thread_title'),
            'url_title' => url_title($this->input->post('thread_title')),
            'thread_desc' => $this->input->post('thread_desc'),
            'thread_content' => $this->input->post('thread_content'),
            'thread_view' => '0',
            'thread_replies' => '0',
            'author_id' => $this->session->userdata('member_id'),       //session
            'forum_title' =>$forum_title,
            'uploaded_files' => $this->input->post('uploaded_files'),
            'enable_notification' => $notification,
            'make_pin' => $pin,
            'make_lock' => $lock,
            'make_annoucement' => $annoucement,
            'reason_content' => '',
            'icon_id' => $this->input->post('icon_id'),
            'make_hidden' => $hidden,
            'latest_entry_date' => now(),
            'posted_date' => now()
        );

        $result=$this->db->insert('board_threads', $insert);
        $data['title']="Information Page";
        $data['header']="Information";


        if($result) {
            $data['time']="2";
            $data['info']="<p>Thread is created Succesfully</p><p>Wait for auto redirecting</p>";
            $data['url']="board/view_thread/$forum_title/". url_title($this->input->post('thread_title'));
        }
        else {
            $data['time']="12";
            $data['info']="<p>Creating Thread Failed</p><p>Wait for auto redirecting</p>";
            $data['url']="users/create_thread/";
        }

        $this->load->view('info',$data);

    }


    /*--------------------------------------------------------------------------------//
		  		 THREAD  MANAGEMENT
    //---------------------------------------------------------------------------------*/

	//Moderator/Global Moderator/Admin can use this function
        //if we have option to give permission to user also
        function delete_thread($thread_id,$page_id)
	{
                $page_id=self::RefinePagNav($page_id);
		$this->db->where('thread_id', $thread_id);
		$query=$this->db->delete('threads');
		//redirect('/admin/Listthreads/'.$pageid, 'refresh');
	}


	//Function Name - UpdateThread
	//This function updates a thread.
	//Parram - threadID and pageID
	//
/**
 * @method edit_thread
 * @param <integer> $thread_id
 * @param <integer> $page_id 
 * @return <string>Info about updation
 */

	function edit_thread($thread_id,$page_id)
	{



				if($this->input->post('enable_emoticons'))
				$enable_emt = 1;
				else
			 	$enable_emt = 0;

				if($this->input->post('enable_signature'))
				$enable_sgn = 1;
				else
			 	$enable_sgn = 0;

				if($this->input->post('enable_notification'))
				$enable_ntfn = 1;
				else
			 	$enable_ntfn = 0;

				if($this->input->post('make_pin'))
				$makepin = 1;
				else
			 	$makepin = 0;

				if($this->input->post('make_annoucement'))
				$make_anct = 1;
				else
			 	$make_anct = 0;

				if($this->input->post('make_lock'))
				$makelock = 1;
				else
			 	$makelock = 0;




						$update = array(
							'thread_title' => $this->input->post('thread_title'),
							'thread_desc' => $this->input->post('thread_desc'),
                			'thread_content' => $this->input->post('post_content'),
							'make_pin' => $makepin,
							'make_lock'=>$makelock,
							'make_annoucement'=>$make_anct,
							'enable_emoticons' => $enable_emt,
							'enable_signature' => $enable_sgn,
							'enable_notification' => $enable_ntfn,

							'icon_id'=>$this->input->post('icon_id'),
							'meta' => $this->input->post('meta'),
							'keyword'=>$this->input->post('keyword'),
							'description'=>$this->input->post('description'),
						   );






				$this->db->where(array('thread_id'=>$thread_id));
	            $check=$this->db->update('threads', $update);

				if($check)
				{
					   //User successfully registered and message generated.
					   $info['message']="<p>Post successfully updated.<p>";
				}
				else
				{
						//User Registration failed due to unknown error.
						//Most common error is wrong database settings.
						$info['message']="Unexpected Error...Updation Failed.";

				}

				$info['title']="Information Page";
				$info['header']="Information";
				$info['url']="/admin/EditUser/".$thread_id;
				$info['time']="2";

				//$this->load->view('info',$info);

      }



 	function list_all_threads($pageid)
	{
		$pageid=self::RefinePagNav($pageid);

    	$this->db->from('threads');
	 	$cnt=$this->db->count_all_results();

		$config['base_url'] = site_url().'/admin/list_all_threads/';
		$config['total_rows'] =$cnt;
		$config['per_page'] = '10';
		$config['num_links'] = '3';
		$config['uri_segment'] = '3';


		$this->pagination->initialize($config);
		$this->db->order_by("posted_date", "desc");
		$query = $this->db->get('threads',$config['per_page'],$this->uri->segment(3));

		$out= "<table width=\"100%\" border=\"0\">";
		$out.="<caption>Thread List</caption>
			<thead>
				<tr>
					<th scope=\"col\">Thread Posted On</th>
					<th scope=\"col\">Member Name</th>
					<th scope=\"col\">Edit</th>
					<th scope=\"col\">Delete</th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<th scope=\"row\">Total</th>

					<td colspan=\"5\">$cnt Posts</td>
				</tr>
			</tfoot>
			<tbody>";

		foreach ($query->result() as $row)
		{
			$out.=
			 "<tr>
			    <td><abbr title=\"$row->posted_date\">".substr($row->posted_date,0,10)."</abbr></td>
			    <td>".$row->author_name."</td>
			    <td>";


				$out.="</td>

			    <td>".anchor('admin/editthread/'.$row->thread_id,'Edit')."</td>
			    <td>".anchor('admin/deletethread/'.$row->thread_id.'/'.$pageid,'Delete')."</td>
			  </tr>";


		}
		$out.= "</tbody></table>";
		$out.= $this->pagination->create_links();
		return $out;

	}


}?>